package com.wuzhishan.news.service.Impl;

import com.wuzhishan.news.mapper.*;
import com.wuzhishan.news.pojo.*;
import com.wuzhishan.news.service.accountService;
import com.wuzhishan.news.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Service
public class accountServiceImpl implements accountService {
    @Autowired
    private favorMapper favorMapper;
    @Autowired
    private articleMapper articleMapper;
    @Autowired
    private userMapper userMapper;
    @Autowired
    private followMapper followMapper;
    @Autowired
    private notifyMapper notifyMapper;
    @Override
    public Result favor(String articleId, HttpSession session) {
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if (user == null){
            //用户未登录
            return Result.of(0);
        }
        BlogFavor favor = favorMapper.getFavorByArticleIdAndUserId(articleId,user.getId());
        if (favor!=null){
            //用户已经喜欢过
            return Result.of(1);
        }
        try {
            favor = new BlogFavor();
            favor.setCreated(DateUtil.now());
            favor.setArticle(articleMapper.getArticleById(articleId));
            favor.setUser(user);
            favorMapper.saveFavor(favor);
            //喜欢成功，给文章作者发通知
            //获取当前文章
            BlogArticle article = articleMapper.getArticleById(articleId);
            BlogNotify notify = new BlogNotify();
            notify.setAvatar(user.getAvatar());
            String content = String.format("喜欢了你的文章 - <a href=\"/view/%s.html\">%s",articleId,article.getTitle());
            notify.setContent(content);
            notify.setCreated(DateUtil.now());
            notify.setTargetId(String.valueOf(article.getUser().getId()));
            notify.setTitle(user.getNickname());
            notify.setUser(user);
            notifyMapper.saveNotify(notify);

            return Result.of(2);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.of(3);
        }
    }

    @Override
    public Result follow(String userId, HttpSession session) {
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if (user == null){
            //用户未登录
            return Result.of(0);
        }
        if (followMapper.isFollow(user.getId(),userId)!=null){
            //已关注状态
            return Result.of(2,"已关注，无需重复关注");
        }
        try {
            BlogFollows follow = new BlogFollows();
            follow.setCreated(DateUtil.now());
            follow.setSource(user);
            BlogUser target = userMapper.getOneById(Long.parseLong(userId));
            follow.setTarget(target);
            followMapper.saveFollow(follow);

            //给关注者发送通知
            BlogNotify notify = new BlogNotify();
            notify.setAvatar(user.getAvatar());
            notify.setContent("关注了你, 你的粉丝+1");
            notify.setCreated(DateUtil.now());
            notify.setTargetId(String.valueOf(target.getId()));
            notify.setTitle(user.getNickname());
            notify.setUser(user);
            notifyMapper.saveNotify(notify);

        } catch (NumberFormatException e) {
            e.printStackTrace();
            return Result.of(2,"服务器错误，请稍后重试!");
        }
        return Result.of(1);
    }

    @Override
    public Result followCheck(String userId, HttpSession session) {
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if (user == null){
            //未登录状态
            return Result.of(0);
        }
        BlogFollows follow = followMapper.isFollow(user.getId(),userId);
        if (follow != null){
            //已关注
            return Result.of(2);
        }
        //未关注
        return Result.of(0);
    }

    @Override
    public Result unfollow(long source_id, String target_id) {
        try {
            followMapper.unFollow(source_id,target_id);
            return Result.of(1,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.of(0,"操作失败").putof("id",target_id);
        }
    }
}
